const app = getApp();
import {
  post
} from '../../utils/request';
import {
  getPlaceInfo,
} from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginInfo: null,
    serviceTel: null,
    myPkg: null,

    counts: null,
    config: null,
  },

  onLogin() {
    app.checkLogin(true, (loginInfo) => {
      this.getCounts();
      this.setData({
        loginInfo
      });
    });
  },

  getCounts() {
    post(`/user/counts`).then(res => {
      if (res.code === 0) {
        this.setData({
          counts: res.data
        });
      }
    });
  },

  // 我的订单
  toMyOrder(e) {
    let {
      status
    } = e.currentTarget.dataset;
    if (undefined === status) {
      status = -1;
    }
    app.checkLogin(true, () => {
      wx.navigateTo({
        url: `/pages/order/my?status=${status}`,
      });
    });
  },

  // 推荐套餐
  toMyPkg() {
    if (!this.data.myPkg || !this.data.myPkg.rawUrl) {
      return;
    }
    app.checkLogin(true, () => {
      wx.navigateTo({
        url: this.data.myPkg.rawUrl,
      });
    });
  },

  onService() {
    if (!this.data.serviceTel) {
      return;
    }
    wx.makePhoneCall({
      phoneNumber: this.data.serviceTel,
    });
  },

  toSetting() {
    wx.navigateTo({
      url: '/pages/setting/index',
    });
  },

  toWallet() {
    app.checkLogin(true, () => {
      wx.navigateTo({
        url: '/pages/wallet/index',
      });
    });
  },

  toPoints() {
    wx.navigateTo({
      url: '/pages/points/index',
    });
  },

  toCoupon() {
    wx.navigateTo({
      url: '/pages/coupon/my',
    });
  },

  toPkg() {
    wx.navigateTo({
      url: '/pages/pkg/index',
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.setBackgroundColor({
      backgroundColor: "#f6f6f6"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    app.getConfig(config => {
      this.setData({
        config
      });
    });

    this.setData({
      loginInfo: app.getLoginInfo()
    });

    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }

    // 客服
    post(`/common/place/${app.globalData.tenantCode}/MP_SVIP_SERVICE_TEL,MP_SVIP_MY_PKG`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            serviceTel: getPlaceInfo(resp.data, 'MP_SVIP_SERVICE_TEL')?.param,
            myPkg: getPlaceInfo(resp.data, 'MP_SVIP_MY_PKG'),
          });
        }
      });

    this.getCounts();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.config.name,
    }
  },

});